package org.jeecg.modules.demo.hydrology.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.hydrology.dto.YslcdssDto;
import org.jeecg.modules.demo.hydrology.dto.YslyjDto;
import org.jeecg.modules.demo.hydrology.entity.Yslcdss;
import org.jeecg.modules.demo.hydrology.entity.Yslyj;
import org.jeecg.modules.demo.hydrology.mapper.CgkcddyMapper;
import org.jeecg.modules.demo.hydrology.mapper.YslyjMapper;
import org.jeecg.modules.demo.hydrology.service.IYslyjService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.mapper.SysDictItemMapper;
import org.jeecg.modules.system.mapper.SysDictMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 涌水量预警
 * @Author: jeecg-boot
 * @Date:   2024-04-17
 * @Version: V1.0
 */
@Service
public class YslyjServiceImpl extends ServiceImpl<YslyjMapper, Yslyj> implements IYslyjService {
    @Autowired
    private YslyjMapper yslyjMapper;
    @Autowired
    private SysDepartMapper sysDepartMapper;
    @Autowired
    private SysDictMapper sysDictMapper;
    @Autowired
    private SysDictItemMapper sysDictItemMapper;
    /**
     * 涌水量预警-分页列表查询
     * @param page
     * @param queryWrapper
     * @return
     */
    @Override
    public IPage<YslyjDto> getList(Page<Yslyj> page, QueryWrapper<Yslyj> queryWrapper) {
        Page<Yslyj> yslyjPage = yslyjMapper.selectPage(page, queryWrapper);
        List<YslyjDto> collect = yslyjPage.getRecords().stream().map(yslyj -> {
            YslyjDto yslyjDto = new YslyjDto();
            BeanUtils.copyProperties(yslyj, yslyjDto);
            QueryWrapper<SysDepart> sysDepartQueryWrapper = new QueryWrapper<>();
            sysDepartQueryWrapper.eq("org_code", yslyj.getStationMine());
            SysDepart sysDepart = sysDepartMapper.selectOne(sysDepartQueryWrapper);
            if (sysDepart != null){
                yslyjDto.setStationName(sysDepart.getDepartName());
            }
            QueryWrapper<SysDict> sysDictQueryWrapper = new QueryWrapper<>();
            sysDictQueryWrapper.eq("dict_code","alarm_status");
            SysDict sysDict = sysDictMapper.selectOne(sysDictQueryWrapper);
            if (sysDict != null){
                QueryWrapper<SysDictItem> sysDictItemQueryWrapper = new QueryWrapper<>();
                sysDictItemQueryWrapper.eq("dict_id",sysDict.getId()).eq("item_value",yslyjDto.getAlarmStatus());
                SysDictItem sysDictItem = sysDictItemMapper.selectOne(sysDictItemQueryWrapper);
                if (sysDictItem != null) {
                    yslyjDto.setAlarmStatusItemText(sysDictItem.getItemText());
                }
            }
            QueryWrapper<SysDict> sysDictQueryWrapper2 = new QueryWrapper<>();
            sysDictQueryWrapper2.eq("dict_code","alarm_level");
            SysDict sysDict2 = sysDictMapper.selectOne(sysDictQueryWrapper2);
            if (sysDict2 != null){
                QueryWrapper<SysDictItem> sysDictItemQueryWrapper1 = new QueryWrapper<>();
                sysDictItemQueryWrapper1.eq("dict_id",sysDict2.getId()).eq("item_value",yslyj.getWarningLevel());
                SysDictItem sysDictItem1 = sysDictItemMapper.selectOne(sysDictItemQueryWrapper1);
                if (sysDictItem1 != null) {
                    yslyjDto.setWarnLevelItemText(sysDictItem1.getItemText());
                }
            }
            return yslyjDto;
        }).collect(Collectors.toList());
        IPage<YslyjDto> yslyjDtoIPage = new Page<>();
        yslyjDtoIPage.setRecords(collect);
        yslyjDtoIPage.setTotal(yslyjPage.getTotal());
        yslyjDtoIPage.setPages(page.getPages());
        yslyjDtoIPage.setSize(page.getSize());
        return yslyjDtoIPage;
    }
}
